package com.gpframework.module.controller.wages;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.wages.domain.GzCompletionSalesTasks;
import com.gpframework.module.wages.domain.GzPersonnelFoundation;
import com.gpframework.module.wages.domain.req.GzCompletionSalesTasksAdd;
import com.gpframework.module.wages.domain.req.GzCompletionSalesTasksReq;
import com.gpframework.module.wages.domain.req.GzCompletionSalesTasksUpdate;
import com.gpframework.module.wages.domain.vo.GzCompletionSalesTasksVo;
import com.gpframework.module.wages.service.IGzCompletionSalesTasksService;
import com.gpframework.module.wages.service.IGzPersonnelFoundationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GzCompletionSalesTasksController
 * @Author gpframework
 * @Date 2024-04-10
 * @Version 1.0.0
 * @Description 年度完成任务(销售任务)Controller
 */
@Validated
@Api(tags = "年度完成任务(销售任务)")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/wages/gzCompletionSalesTasks" )
public class GzCompletionSalesTasksController extends BaseController {

    private final IGzPersonnelFoundationService iGzPersonnelFoundationService;
    private final IGzCompletionSalesTasksService iGzCompletionSalesTasksService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-04-10
  * @Param gzCompletionSalesTasks
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    //@PreAuthorize("@ss.hasPermi('wages:gzCompletionSalesTasks:list')")
    @GetMapping("/list")
    public TableDataInfo<GzCompletionSalesTasksVo> list(GzCompletionSalesTasksReq gzCompletionSalesTasksReq)
    {
        startPage();
        QueryWrapper<GzCompletionSalesTasks> lqw = new QueryWrapper<GzCompletionSalesTasks>();
        gzCompletionSalesTasksReq.generatorQuery(lqw,true);
        List<GzCompletionSalesTasksVo> list = iGzCompletionSalesTasksService.listVo(lqw);
        return getDataTable(list);
    }


    @ApiOperation("统计档口工资列表")
    //@PreAuthorize("@ss.hasPermi('wages:gzCompletionSalesTasks:list')")
    @GetMapping("/listed")
    public TableDataInfo<GzCompletionSalesTasksVo> listed(GzCompletionSalesTasksReq gzCompletionSalesTasksReq)
    {
        startPage();
        QueryWrapper<GzCompletionSalesTasks> lqw = new QueryWrapper<GzCompletionSalesTasks>();
        gzCompletionSalesTasksReq.generatorQuery(lqw,true);
        List<GzCompletionSalesTasksVo> list = iGzCompletionSalesTasksService.listVoed(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-04-10
     * @Param gzCompletionSalesTasks
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    //@PreAuthorize("@ss.hasPermi('wages:gzCompletionSalesTasks:add')" )
    @Log(title = "添加年度完成任务(销售任务)" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GzCompletionSalesTasksAdd gzCompletionSalesTasksAdd) {
        GzCompletionSalesTasks gzCompletionSalesTasks = Convert.convert(new TypeReference<GzCompletionSalesTasks>() {}, gzCompletionSalesTasksAdd);
        return toAjax(iGzCompletionSalesTasksService.save(gzCompletionSalesTasks) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-04-10
     * @Param gzCompletionSalesTasks
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    //@PreAuthorize("@ss.hasPermi('wages:gzCompletionSalesTasks:edit')" )
    @Log(title = "修改年度完成任务(销售任务)" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GzCompletionSalesTasksUpdate gzCompletionSalesTasksUpdate) {
        GzCompletionSalesTasks gzCompletionSalesTasks = Convert.convert(new TypeReference<GzCompletionSalesTasks>() {}, gzCompletionSalesTasksUpdate);
        return toAjax(iGzCompletionSalesTasksService.updateById(gzCompletionSalesTasks) ? 1 : 0);
    }


    @ApiOperation("批量修改")
    //@PreAuthorize("@ss.hasPermi('wages:gzCompletionSalesTasks:edit')" )
    @Log(title = "批量修改年度完成任务(销售任务)" , businessType = BusinessType.UPDATE)
    @PutMapping("/editBatch")
    public AjaxResult edit(@Valid @RequestBody List<GzCompletionSalesTasksUpdate> gzCompletionSalesTasksUpdateList) {

        List<GzCompletionSalesTasks> convert = Convert.convert(new TypeReference<List<GzCompletionSalesTasks>>() {
        }, gzCompletionSalesTasksUpdateList);
        return toAjax(iGzCompletionSalesTasksService.updateBatchById(convert) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-04-10
     * @Param gzCompletionSalesTasks
     * @Return TableDataInfo
     */

    //@PreAuthorize("@ss.hasPermi('wages:gzCompletionSalesTasks:export')" )
    @Log(title = "导出年度完成任务(销售任务)" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GzCompletionSalesTasksReq gzCompletionSalesTasksReq) {
        QueryWrapper<GzCompletionSalesTasks> lqw = new QueryWrapper<GzCompletionSalesTasks>();
        gzCompletionSalesTasksReq.generatorQuery(lqw,true);
        List<GzCompletionSalesTasksVo> list = iGzCompletionSalesTasksService.listVo(lqw);
        ExcelUtil<GzCompletionSalesTasksVo> util = new ExcelUtil<GzCompletionSalesTasksVo>(GzCompletionSalesTasksVo. class);
        return util.exportExcel(list, "gzCompletionSalesTasks" );
    }



    @ApiOperation("导入年度完成任务(销售任务)")
    @Log(title = "导入年度完成任务(销售任务)" , businessType = BusinessType.INSERT)
    @PostMapping("/import")
    public AjaxResult importList(MultipartFile file) throws Exception {
        if (file == null) {
            return AjaxResult.error("获取不到导入文件");
        }
        boolean excel = ExcelUtil.isExcel(file.getOriginalFilename());
        if (!excel) {
            return AjaxResult.error("导入文件格式错误，请导入excel文件");
        }
        // 导入新数据
        ExcelUtil<GzCompletionSalesTasksVo> util = new ExcelUtil<GzCompletionSalesTasksVo>(GzCompletionSalesTasksVo.class);
        List<GzCompletionSalesTasksVo> excelList = util.importCacheExcel(file.getInputStream());
        List<GzCompletionSalesTasks> list = Convert.convert(new TypeReference<List<GzCompletionSalesTasks>>() {}, excelList);

        for (int i = 0; i < list.size(); i++) {
            GzPersonnelFoundation gzPersonnelFoundation = iGzPersonnelFoundationService.getOne(new QueryWrapper<GzPersonnelFoundation>()
                    .eq("name", list.get(i).getName())
                    .eq("department_name", list.get(i).getDepartmentName())
                    .eq("del_status", YesNoEnum.NO.getKey()));


            GzCompletionSalesTasks  gzCompletionSalesTasks= iGzCompletionSalesTasksService.getOne(new QueryWrapper<GzCompletionSalesTasks>()
                    .eq("name", list.get(i).getName())
                    .eq("department_name", list.get(i).getDepartmentName())
                    .eq("month", list.get(i).getMonth())
                    .eq("del_status", YesNoEnum.NO.getKey()));
            if (!StringUtils.isEmpty(gzCompletionSalesTasks))
            {
                list.get(i).setId(gzCompletionSalesTasks.getId());
            }

            list.get(i).setUserId(gzPersonnelFoundation.getId());
        }

        return iGzCompletionSalesTasksService.saveOrUpdateBatch(list) ? AjaxResult.success("导入成功") : AjaxResult.error();
    }


    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-04-10
     * @Param gzCompletionSalesTasks
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    //@PreAuthorize("@ss.hasPermi('wages:gzCompletionSalesTasks:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GzCompletionSalesTasksVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGzCompletionSalesTasksService.getVoById(id));
    }



    @ApiOperation("生成年度完成任务(销售任务)")
    @Log(title = "生成年度完成任务(销售任务)" , businessType = BusinessType.INSERT)
    @PostMapping("/completionSalesTasks")
    public AjaxResult completionSalesTasks(@RequestBody GzCompletionSalesTasksReq gzCompletionSalesTasksReq) throws ParseException {
        List<GzPersonnelFoundation> gzPersonnelFoundations = iGzPersonnelFoundationService.lambdaQuery().eq(GzPersonnelFoundation::getDepartmentType, 1)
                .ne(GzPersonnelFoundation::getDepartmentName, "办公室")
                .list();
        iGzCompletionSalesTasksService.remove(new QueryWrapper<GzCompletionSalesTasks>().eq("month", gzCompletionSalesTasksReq.getMonth()));
        List<GzCompletionSalesTasks> gzCompletionSalesTasks = new ArrayList<>();

        for (GzPersonnelFoundation gzPersonnelFoundation : gzPersonnelFoundations) {
            GzCompletionSalesTasks gzCompletionSalesTask = new GzCompletionSalesTasks();
            gzCompletionSalesTask.setMonth(gzCompletionSalesTasksReq.getMonth());
            gzCompletionSalesTask.setDepartmentName(gzPersonnelFoundation.getDepartmentName());
            gzCompletionSalesTask.setUserId(gzPersonnelFoundation.getId());
            gzCompletionSalesTask.setName(gzPersonnelFoundation.getName());

            gzCompletionSalesTasks.add(gzCompletionSalesTask);
        }
        return AjaxResult.success(iGzCompletionSalesTasksService.saveBatch(gzCompletionSalesTasks)?1:0);
    }





        /**
         * @Description 删除
         * @Author gpframework
         * @Date 2024-04-10
         * @Param gzCompletionSalesTasks
         * @Return TableDataInfo
         */
    @ApiOperation("删除")
    //@PreAuthorize("@ss.hasPermi('wages:gzCompletionSalesTasks:remove')" )
    @Log(title = "删除年度完成任务(销售任务)" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGzCompletionSalesTasksService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
